home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8161 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help me elect rounding of int division
  5. Date: 28 Feb 96 00:03:52 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.825465832@rscernix>
  8. References: <4grtbc$t9f@chaos.kulnet.kuleuven.ac.be>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4grtbc$t9f@chaos.kulnet.kuleuven.ac.be> Andreas.DeTroy@ped.kuleuven.ac.be (Andreas De Troy) writes:
  13.  
  14. >How about this:
  15. >
  16. >#include <float.h>
  17.           ^^^^^^^^^
  18. Make it <math.h>.  Your code doesn't need the stuff from <float.h> and
  19. floor() is declared in <math.h>, not in <float.h>.
  20.  
  21. >long rounddiv (long a, long b)
  22. >{
  23. >    return ((long) (floor (((float) a / b) + 0.5)));
  24.              ^^^^^^
  25. What's the point of this cast?  The compiler knows that it has to return
  26. a long and it also knows how to obtain one from a double value, it doesn't
  27. hints from you :-)
  28.  
  29. The only thing achieved by the usage of unnecessary casts and the
  30. excessive use of unneeded parentheses is a reduction in the readability
  31. and maintainability of the code.
  32.  
  33.     return floor((float) a / b + 0.5);
  34.  
  35. looks much cleaner (and it saves a lot of typing :-)
  36.  
  37. I didn't address the correctness of the rounding algorithm used, because
  38. the subject has been already beaten to death in this newsgroup.
  39.  
  40. Dan
  41. --
  42. Dan Pop
  43. CERN, CN Division
  44. Email: danpop@mail.cern.ch 
  45. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  46.